local db = TUNING.MOD_AVATAR_BH_MOBIUS.weapon.bh_mobius_sword.lvlup


---@class bh_mobius_sword_lvlup_data 
---@field apply nil|fun(inst:ent,owner:ent) # 武器再获取物品时的回调函数

---@type table<PrefabID,bh_mobius_sword_lvlup_data>
local apply_data = {
    goldnugget = {
        apply = function (inst, owner)
            if inst and inst.components.weapon then
                local delta = inst.components.bh_mobius_sword_lvlup and inst.components.bh_mobius_sword_lvlup:Get('goldnugget') or 0
                local damage = inst.components.weapon.damage
                local res = damage + db.goldnugget.dmg
                if res > 0 then
                    inst.components.weapon:SetDamage(res)
                end
            end
        end
    },
    walrus_tusk = {},
    stinger = {
        apply = function (inst, owner)
            if inst and inst.components.bh_mobius_sword_lvlup and inst.components.equippable and inst.components.equippable:IsEquipped() and owner and owner.components.combat then
                local speed = inst.components.bh_mobius_sword_lvlup and inst.components.bh_mobius_sword_lvlup:Get('stinger') or 0
                if speed > 0 then
                    owner.components.combat:SetAtkPeriodModifier(inst,1+db.stinger.atkperiod*speed,'bh_mobius_sword_lvlup_stinger')
                end
            end
        end
    },
}


local props = {}

for k,_ in pairs(apply_data) do
    props[k] = function (self,value)
        self.inst.replica.bh_mobius_sword_lvlup[k]:set(value)
    end
end

---@class components
---@field bh_mobius_sword_lvlup component_bh_mobius_sword_lvlup

-- local function on_val(self, value)
    -- self.inst.replica.bh_mobius_sword_lvlup:SetVal(value)
-- end

---@class component_bh_mobius_sword_lvlup
---@field inst ent
local bh_mobius_sword_lvlup = Class(

---@param self component_bh_mobius_sword_lvlup
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0
    for k,_ in pairs(apply_data) do
        self[k] = 0
    end
end,
nil,
props)

function bh_mobius_sword_lvlup:OnSave()
    local data = {}
    for k,_ in pairs(apply_data) do
        data[k] = self[k] or 0
    end
    return data
end

function bh_mobius_sword_lvlup:OnLoad(data)
    -- self.val = data.val or 0
    for k,v in pairs(data or {}) do
        self[k] = v or 0
    end
end

function bh_mobius_sword_lvlup:IsMax(prefab)
    return self[prefab] >= db[prefab].max
end

function bh_mobius_sword_lvlup:AddItem(prefab)
    if self[prefab] and not self:IsMax(prefab) then
        self[prefab] = self[prefab] + 1
        self:Apply(prefab)
    end
end

function bh_mobius_sword_lvlup:Apply(prefab)
    if apply_data[prefab] and apply_data[prefab].apply then
        local owner = self.inst.components.inventoryitem and self.inst.components.inventoryitem.owner
        if owner then
            apply_data[prefab].apply(self.inst,owner)
        end
    end
end

function bh_mobius_sword_lvlup:Get(prefab)
    return self[prefab] or 0
end

return bh_mobius_sword_lvlup